package com.zishi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zishi.model.vo.ReportVO;
import com.zishi.model.dto.ReportDTO;
import com.zishi.model.pojo.Report;
import com.zishi.model.pojo.User;
import com.zishi.mapper.ReportMapper;
import com.zishi.mapper.UserMapper;
import com.zishi.service.ReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zishi.util.PartitionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 报告表 服务实现类
 * </p>
 *
 * @author TP
 * @since 2022-05-05 08:53:41
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 将Reprot的内容补全(用户信息补全): Report = ReportDTO + User的部分字段
     * @param reportDTO
     * @return 返回插入的report的主键id 或 失败 -1
     */
    @Override
    public Integer saveOneReport(ReportDTO reportDTO) {
        Report report = new Report();
        User user = userMapper.selectById(reportDTO.getUserId());

        BeanUtils.copyProperties(reportDTO, report);

//        report.setPostureType(reportDTO.getPostureType());
//        report.setProblem(reportDTO.getProblem());
//        report.setPicture(reportDTO.getPicture());
//        report.setArticleId(reportDTO.getArticleId());
//        report.setUserId(reportDTO.getUserId());

//        report.setUserName(user.getName());
//        report.setFavicon(user.getFavicon());
//        report.setSex(user.getSex());
//        report.setAge(user.getAge());
        int insert = reportMapper.insert(report);
        System.out.println("\n\n1111111111"+ insert +"\n\n\n");
        return  insert > 0 ? report.getId(): -1;
    }


    @Override
    public List<ReportVO> getAllReportByUserId(Integer userId) {
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Report> list = list(queryWrapper);
        Iterator<Report> iterator = list.iterator();

        User user = userMapper.selectById(userId);

        Report report;
        List<ReportVO> reportVOs = new ArrayList<>();
        while (iterator.hasNext()) {
            report = iterator.next();
            if (report == null) {
                continue;
            }
           /*
           // XXX: 下次有空再来进行转换
            cgilb 的BeanCopier使用失败,下次再来试试
            ReportVO reportVO = new ReportVO();
            BeanCopier.create(report.getClass(), reportVO.getClass(), false);
            reportVO.setArticleId(PartitionUtil.separateCharacters(report.getArticleId()));
            */

            ReportVO reportVO = new ReportVO(report.getId(),userId, user.getName(), user.getFavicon(),
                    user.getSex(), user.getAge(), report.getPostureType(), report.getProblem(), report.getScore(),
                    PartitionUtil.separateCharacters(report.getPicture()),
                    PartitionUtil.separateCharacters(report.getReturnPicture()),
                    PartitionUtil.separateCharacters(report.getArticleId(), 1), report.getGmtCreate());

            reportVOs.add(reportVO);
        }
        return reportVOs;
    }


    /**
     * 根据报告id获取报告
     */
    @Override
    public ReportVO getReportById(Integer id) {
        Report report = reportMapper.selectById(id);
        if (report == null) {
            return null;
        }
        User user = userMapper.selectById(report.getUserId());


        return new ReportVO(report.getId(),user.getId(), user.getName(), user.getFavicon(),
                user.getSex(), user.getAge(), report.getPostureType(), report.getProblem(), report.getScore(),
                PartitionUtil.separateCharacters(report.getPicture()),
                PartitionUtil.separateCharacters(report.getReturnPicture()),
                PartitionUtil.separateCharacters(report.getArticleId(), 1), report.getGmtCreate());
    }

    @Override
    public void updatePicture(Integer id, String pictureUrl) {
        reportMapper.updatePicture(id, pictureUrl);
    }
}
